import java.applet.Applet;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JSlider;
import javax.swing.JToolBar;
import javax.swing.ListCellRenderer;
import javax.swing.undo.CannotRedoException;

public class ToolBarLogo extends JToolBar implements ActionListener, MouseListener{
	
	private Logo logo;
	
	private JToolBar barreExecution;
		
	private JButton Bsave,BzoomM,BzoomP,Bhautparleur,Banimal,Btelecommande,BImprimante,BQuadrillage,Bstop;
	public JButton BUndo,BRedo;
	private JComboBox Couleur;
	private JSlider Cvitesse;
	private ImageIcon[] images;
		
	private Font fondToolBar;
	private JButton BClear;
	public static boolean sonOn = true;
	
	public ToolBarLogo(Logo logo) {
		super();
		this.logo=logo;
		
		barreExecution = new JToolBar();
		
		//Chargement image comboCouleur
		
		
		/*****ToolbarEditeur*****/
		Btelecommande = new JButton();
		Btelecommande.setIcon(new ImageIcon(Logo.Itelecommande));
		Btelecommande.addActionListener(this);
		Btelecommande.setBorderPainted(false);
		Btelecommande.setToolTipText("Activer/Desactiver la telecommande");
		BImprimante = new JButton();
		BImprimante.setIcon(new ImageIcon(Logo.Iprint));
		BImprimante.addActionListener(this);
		BImprimante.setBorderPainted(false);
		BImprimante.setToolTipText("Imprimer");
		BUndo = new JButton();
		BUndo.setIcon(new ImageIcon(Logo.Iundo));
		BUndo.addActionListener(this);
		BUndo.setBorderPainted(false);
		BUndo.setEnabled(false);
		BUndo.setToolTipText("Annuler");
		BRedo = new JButton();
		BRedo.setIcon(new ImageIcon(Logo.Iredo));
		BRedo.addActionListener(this);
		BRedo.setBorderPainted(false);
		BRedo.setEnabled(false);
		BRedo.setToolTipText("Refaire");
		
		//ajout des boutons
		this.add(Btelecommande);
		this.add(BImprimante);
		this.add(BUndo);
		this.add(BRedo);
		/*****ToolbarEditeur*****/
		
		this.add(Box.createRigidArea(new Dimension(90, 0))); //espace boutons JToolBar
		
		/*****ToolbarExecution*****/	
		Bsave = new JButton();
		Bsave.setIcon(new ImageIcon(Logo.IsaveIMG));
		Bsave.addActionListener(this);
		Bsave.setBorderPainted(false);
		Bsave.setToolTipText("Enregistrer l'image");
		Banimal = new JButton();
		Banimal.setIcon(new ImageIcon(Logo.Ianimaltortue));
		Banimal.setBorderPainted(false);
		Banimal.addActionListener(this);
		Banimal.setToolTipText("Activer/Desactiver l'affichage de la tortue");		
		BzoomM = new JButton();
		BzoomM.setIcon(new ImageIcon(Logo.IzoomM));
		BzoomM.setBorderPainted(false);
		BzoomM.addActionListener(this);
		BzoomM.setToolTipText("Dezoomer");
		BzoomP = new JButton();
		BzoomP.setIcon(new ImageIcon(Logo.IzoomP));
		BzoomP.setBorderPainted(false);
		BzoomP.addActionListener(this);
		BzoomP.setToolTipText("Zoomer");
		Bhautparleur = new JButton();
		Bhautparleur.setIcon(new ImageIcon(Logo.Ihp));
		Bhautparleur.setBorderPainted(false);
		Bhautparleur.addActionListener(this);
		Bhautparleur.setToolTipText("Activer/Desactiver le son");
		Bstop = new JButton();
		Bstop.setIcon(new ImageIcon(Logo.Istop));
		Bstop.setBorderPainted(false);
		Bstop.addActionListener(this);
		Bstop.setToolTipText("Arretez l'animation");		
		BClear = new JButton();
		BClear.setIcon(new ImageIcon(Logo.Igomme));
		BClear.setBorderPainted(false);
		BClear.addActionListener(this);
		BClear.setToolTipText("Effacer le panneau");
		BQuadrillage=new JButton();
		BQuadrillage.setIcon(new ImageIcon(Logo.Iquad));
		BQuadrillage.addActionListener(this);
		BQuadrillage.setToolTipText("Afficher le quadrillage");
		BQuadrillage.setBorderPainted(false);

		//on definit un style		
		fondToolBar = new Font("Arial",Font.BOLD,10);
		
		//combobox vitesse
		Cvitesse=new JSlider(1,5,3);	
		Cvitesse.setSize(100,50);
		Hashtable labelTable = new Hashtable();
		labelTable.put( new Integer( 1 ), new JLabel("-") );
		labelTable.put( new Integer( 5 ), new JLabel("+") );
		Cvitesse.setLabelTable( labelTable );
		Cvitesse.setFont(fondToolBar);
		Cvitesse.setMajorTickSpacing(1);
		//Cvitesse.setPaintTicks(true);
		Cvitesse.setPaintLabels(true);
		Cvitesse.setSnapToTicks(true);
		Cvitesse.setBorder(BorderFactory.createTitledBorder(null,"Vitesse",0,0,fondToolBar));
		Cvitesse.setToolTipText("Changer la vitesse de l'animation");
		Cvitesse.setPreferredSize(new Dimension(50,35));
		Cvitesse.addMouseListener(this);
		
		//on definit la comboBox associe a la couleur du fond
		images = new ImageIcon[logo.imagePalette.length];
        Integer[] intArray = new Integer[logo.imagePalette.length];
        for (int i = 0; i < logo.imagePalette.length; i++) {
            intArray[i] = new Integer(i);
            images[i] = new ImageIcon(logo.imagePalette[i]);          
        }                     
        Couleur = new JComboBox(intArray);
        ComboBoxRenderer renderer= new ComboBoxRenderer();        
        Couleur.setRenderer(renderer);
        Couleur.setMaximumRowCount(6);
        Couleur.setBorder(BorderFactory.createTitledBorder(null,"Couleur fond",0,0,fondToolBar));
		Couleur.addActionListener(this);
		Couleur.setToolTipText("Changer la couleur du fond");
		
		//ajout des boutons
		barreExecution.add(Bsave);
		barreExecution.add(Banimal);
		barreExecution.add(BQuadrillage);
		barreExecution.add(BzoomM);
		barreExecution.add(BzoomP);
		barreExecution.add(Couleur);
		barreExecution.add(Cvitesse);				
		//barreExecution.add(Bhautparleur);
		barreExecution.add(Bstop);
		barreExecution.add(BClear);		
		/*****ToolbarExecution*****/
		
		
		this.setBackground(new Color(240,240,240));
		barreExecution.setBackground(new Color(240,240,240));
		
		this.add(barreExecution);		
	}

	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==Btelecommande){			
			logo.setTelecommande();			
		}		
		if(e.getSource()==Bsave){
			
			logo.getExec().Enregistrer();
			logo.getConsol().sys("image enregistree");	
		}
		if(e.getSource()==Banimal){
			logo.getExec().setAffiche(!logo.getExec().getAffiche());
			if (logo.getExec().getAffiche())
			{
				logo.getConsol().sys("affichage animal ON");
				logo.getExec().setTortue(
						logo.getExec().getPoint(),
						logo.getExec().getOrientation(),
						true
						);
			}
			else{
				logo.getConsol().sys("affichage animal OFF");
				logo.getExec().setTortue(
						logo.getExec().getPoint(),
						logo.getExec().getOrientation(),
						false
						);
			}
				
		}		
		if(e.getSource()==BzoomP){
			logo.getConsol().sys("Zoom +");
			logo.getExec().zoom*=logo.getExec().vitesseZoom;
			logo.getExec().decalageX-=(logo.getExec().getTortue().getX()/logo.getExec().vitesseZoom/logo.getExec().zoom)+logo.getExec().getTortue().getX()/logo.getExec().vitesseZoom/logo.getExec().zoom;
			logo.getExec().decalageY-=(logo.getExec().getTortue().getY()/logo.getExec().vitesseZoom/logo.getExec().zoom)+logo.getExec().getTortue().getY()/logo.getExec().vitesseZoom/logo.getExec().zoom;
			logo.getExec().repaint();
		}
		
		if(e.getSource()==BzoomM && logo.getExec().zoom>1){
			logo.getConsol().sys("Zoom -");			
			logo.getExec().decalageX+=(logo.getExec().getTortue().getX()/logo.getExec().vitesseZoom/logo.getExec().zoom)+logo.getExec().getTortue().getX()/logo.getExec().vitesseZoom/logo.getExec().zoom;
			logo.getExec().decalageY+=(logo.getExec().getTortue().getY()/logo.getExec().vitesseZoom/logo.getExec().zoom)+logo.getExec().getTortue().getY()/logo.getExec().vitesseZoom/logo.getExec().zoom;
			logo.getExec().zoom/=logo.getExec().vitesseZoom;
			logo.getExec().repaint();
		}
		
		if(e.getSource()==Bstop){
			logo.getExec().StopDeplacement();
		}
		
		if(e.getSource()==Bhautparleur){
			if(sonOn)
			{Bhautparleur.setIcon(new ImageIcon(Logo.IhpOff));
			 sonOn=false;
			 logo.getConsol().sys("Hautparleur OFF");
			}
			else
			{Bhautparleur.setIcon(new ImageIcon(logo.Ihp));
			 sonOn=true;
			 logo.getConsol().sys("Hautparleur ON");
			}
		}
		if(e.getSource() == BImprimante){
			new ImpressionLogo(logo).setVisible(true);
			logo.getConsol().sys("Impression en cours...");
		}
		if(e.getSource()==BUndo){
			try {
		          logo.getEdit().getJTexteLogo().undoManager.undo();
		        } catch (CannotRedoException cre) {
		          cre.printStackTrace();
		        }
		        logo.updateButtonsUndoRedo();
		      }		
		if(e.getSource()==BRedo){
			try {
					logo.getEdit().getJTexteLogo().undoManager.redo();
		        } catch (CannotRedoException cre) {
		          cre.printStackTrace();
		        }
		        logo.updateButtonsUndoRedo();
		}		
		if(e.getSource()==BClear){
			logo.getExec().clearScreen();
			logo.getExec().repaint();
		}
		if(e.getSource()==BQuadrillage){
			logo.getExec().setQuadrillage(!logo.getExec().getQuadrillage());
			if (logo.getExec().getQuadrillage())
			{
				logo.getConsol().sys("Quadrillage ON");
			}
			else logo.getConsol().sys("Quadrillage OFF");		
		}		
		if(e.getSource()==Couleur){
			if(Couleur.getSelectedIndex()==0){
				logo.getConsol().sys("Fond blanc");
				logo.getExec().setBackground(Color.WHITE);
			}			
			if(Couleur.getSelectedIndex()==1){
				logo.getConsol().sys("Fond bleu");
				logo.getExec().setBackground(Color.BLUE);
			}			
			if(Couleur.getSelectedIndex()==2){
				logo.getConsol().sys("Fond jaune");
				logo.getExec().setBackground(Color.YELLOW);
			}			
			if(Couleur.getSelectedIndex()==3){
				logo.getConsol().sys("Fond noir");
				logo.getExec().setBackground(Color.BLACK);
			}			
			if(Couleur.getSelectedIndex()==4){
				logo.getConsol().sys("Fond rouge");
				logo.getExec().setBackground(Color.RED);
			}			
			if(Couleur.getSelectedIndex()==5){
				logo.getConsol().sys("Fond vert");
				logo.getExec().setBackground(Color.GREEN);
			}				
		}
		
	}
	
	public void mouseClicked(MouseEvent arg0) {}
	public void mousePressed(MouseEvent arg0) {}
	public void mouseEntered(MouseEvent arg0) {}
	public void mouseExited(MouseEvent arg0) {}
	public void mouseReleased(MouseEvent arg0) {
		switch (Cvitesse.getValue())
		{
		case 1: logo.getExec().setVitesse(1);; break;
		case 2: logo.getExec().setVitesse(5);; break;
		case 3: logo.getExec().setVitesse(20);; break;
		case 4: logo.getExec().setVitesse(50);; break;
		case 5: logo.getExec().setVitesse(500);; break;
		default: break;
		}
		logo.getConsol().sys("Vitesse : "+Cvitesse.getValue());		
	}
	

	
	class ComboBoxRenderer extends JLabel implements ListCellRenderer 
 	{
        private Font uhOhFont;

        public ComboBoxRenderer() {
            setOpaque(true);
            setHorizontalAlignment(CENTER);
            setVerticalAlignment(CENTER);
        }

	public Component getListCellRendererComponent(
                                           JList list,
                                           Object value,
                                           int index,
                                           boolean isSelected,
                                           boolean cellHasFocus) {
         
           int selectedIndex = ((Integer)value).intValue();


            if (isSelected) {
                setBackground(list.getSelectionBackground());                
                setForeground(list.getSelectionForeground());
            } else {
                setBackground(list.getBackground());
                setForeground(list.getForeground());
                
            }
       
                      
            ImageIcon icon = images[selectedIndex];            
            setIcon(icon);
            if (icon != null) 
            	setFont(list.getFont());           
            	
            	
            return this;
        }
    }		

}
